
package view.additional;

import com.sun.lwuit.Display;
import com.sun.lwuit.Font;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.List;
import com.sun.lwuit.animations.Motion;
import com.sun.lwuit.list.DefaultListModel;
import com.sun.lwuit.list.ListModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**

* This class must be the top level scrollable to work propely, all of its parent containers

* must be scrollable false!

*

* @author Shai Almog

*/

public class ThumbList extends List {

  /**

   * Delay for the thumb to start "returning" from the moment the user released the touch screen.

   */

  private static final int THUMB_SLIDEBACK_DELAY = 2200;



  /**

   * Duration for the slide animation

   */

  private static final int THUMB_SLIDE_DURATION = 300;



  /**

   * Indicates whether the thumb image is is showing

   */



  /**

   * Flags for thumb slide timeout

   */

  private long thumbTimerStartTime;

  private int thumbTimer = -1;



  /**

   * Animation motion returning the thumb to its "place"

   */

  private Motion thumbSlidebackMotion;



  /**

   * Thumb coordinates on the screen, the X isn't the real X since the width should be added

   */

  private int thumbPositionX;

  private int thumbPositionY;



  /**

   * Flag indicating that we are now dragging via the thumb and not the gesture

   */

  private boolean thumbDragMode;



  /**

   * Background image for the letter displayed on the screen

   */

  private Image transparentRoundRect;



  /**

   * Font used for the letter on the screen during thumb drag mode

   */

  private Font largeFont = Font.createSystemFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);


  public ThumbList()
  {
      this(new DefaultListModel());
  }

  public ThumbList(ListModel model)
  {
      super(model);

      setSmoothScrolling(Display.getInstance().isTouchScreenDevice());

      int size = largeFont.charWidth('W') * 3;

      transparentRoundRect = Image.createImage(size, size);

      Image mask = Image.createImage(size, size);

      Graphics g = mask.getGraphics();

      g.setColor(0);

      g.fillRect(0, 0, size, size);

      g.setColor(0x999999);

      g.fillRoundRect(0, 0, size, size, 12, 12);

      g = transparentRoundRect.getGraphics();

      g.setColor(0xffffff);

      g.fillRoundRect(0, 0, size, size, 12, 12);

      g.setColor(0);

      g.fillRoundRect(2, 2, size - 4, size - 4, 12, 12);

      transparentRoundRect = transparentRoundRect.applyMask(mask.createMask());

  }



  /**

   * We must register as an animated otherwise the thumb won't get callbacks to slide back into place

   */

  protected void initComponent() {

      getComponentForm().registerAnimated(this);

  }



  protected void deinitialize() {

      getComponentForm().deregisterAnimated(this);

  }

  /**

   * Overriding the press to detect thumb presses and to start showing the thumb

   */

  public void pointerPressed(int x, int y)
  {
      thumbSlidebackMotion = null;

      thumbTimer = -1;

      int myY = y - getAbsoluteY() - getScrollY();

      int myX = x - getAbsoluteX() - getScrollX();

      super.pointerPressed(x, y);

  }



  /**

   * We block pointer events from the list when in thumb drag mode and move the list

   * ourselves in this method

   */

  public void pointerDragged(int x, int y)
  {
          super.pointerDragged(x, y);
  }



  /**

   * We block pointer events from the list when in thumb drag mode, we activate the animation

   * to hide the thumb

   */

  public void pointerReleased(int x, int y) {

      if(thumbDragMode) {

          thumbDragMode = false;

          repaint();

      } else {

          super.pointerReleased(x, y);

      }

      thumbTimerStartTime = System.currentTimeMillis();

      thumbTimer = THUMB_SLIDEBACK_DELAY;

  }

  /**

   * We don't need to override the actual paint method since we must draw our own scrollbar

   */

  protected void paintScrollbarY(Graphics g) {

      super.paintScrollbarY(g);
  }

  /**

   * Update thumb animation state

   */

  public boolean animate() {

      boolean v = super.animate();

      if(thumbTimer > 0) {

          long t = System.currentTimeMillis();

          thumbTimer = THUMB_SLIDEBACK_DELAY - ((int)(t - thumbTimerStartTime));

          v = true;

      } else {

          if(thumbSlidebackMotion != null) {

              thumbPositionX = thumbSlidebackMotion.getValue();

              if(thumbSlidebackMotion.isFinished()) {

                  thumbSlidebackMotion = null;

              }



              // we still want to return true to render the last frame for a finished motion

              v = true;

          }

      }

      return v;
  }   
}

